package models

import (
	"errors"
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cast"
	"gorm.io/gorm"
	"hc_server/routers/response/agentResponse"
	"time"
)

// AgentSettlementRate 代理商结算费率
type AgentSettlementRate struct {
	Model
	AgentId             int     `json:"agent_id" gorm:"comment:'代理商ID'"`
	Type                int     `json:"type" gorm:"default:0;comment:'是否编辑过 0=未编辑过 1=编辑过'"`
	D0SettlementRateMin float64 `json:"d0_settlement_rate_min" gorm:"type:decimal(10,6);comment:'D0付款结算分润百分比 最小值'"`
	D0SettlementRateMax float64 `json:"d0_settlement_rate_max" gorm:"type:decimal(10,6);comment:'D0付款结算分润百分比 最大值'"`
	D0SettlementMin     float64 `json:"d0_settlement_min" gorm:"type:decimal(10,6);comment:'D0付款结算分润手续费 最小值'"`
	D0SettlementMax     float64 `json:"d0_settlement_max" gorm:"type:decimal(10,6);comment:'D0付款结算分润手续费 最大值'"`
	D1SettlementRateMin float64 `json:"d1_settlement_rate_min" gorm:"type:decimal(10,6);comment:'D1付款结算分润百分比 最小值'"`
	D1SettlementRateMax float64 `json:"d1_settlement_rate_max" gorm:"type:decimal(10,6);comment:'D1付款结算分润百分比 最大值'"`
	D1SettlementMin     float64 `json:"d1_settlement_min" gorm:"type:decimal(10,6);comment:'D1付款结算分润手续费 最小值'"`
	D1SettlementMax     float64 `json:"d1_settlement_max" gorm:"type:decimal(10,6);comment:'D1付款结算分润手续费 最大值'"`
	WithdrawalRateMin   float64 `json:"withdrawal_rate_min" gorm:"type:decimal(10,6);comment:'提现费率百分比 最小值'"`
	WithdrawalRateMax   float64 `json:"withdrawal_rate_max" gorm:"type:decimal(10,6);comment:'提现费率百分比 最大值'"`
	WithdrawalMin       float64 `json:"withdrawal_min" gorm:"type:decimal(10,6);comment:'提现手续费 最小值'"`
	WithdrawalMax       float64 `json:"withdrawal_max" gorm:"type:decimal(10,6);comment:'提现手续费 最大值'"`
}

// AgentSettlementRateAddBaseSettlementInfo 添加代理基础结算信息
func AgentSettlementRateAddBaseSettlementInfo(agentId, superiorId interface{}) error {
	var settlementRate AgentSettlementRate
	db.Model(&AgentSettlementRate{}).Where("agent_id = ?", superiorId).Scan(&settlementRate)
	settlementRate.AgentId = cast.ToInt(agentId)
	settlementRate.CreatedAt = time.Now()
	settlementRate.UpdatedAt = time.Now()
	settlementRate.Type = 0
	settlementRate.ID = 0

	err := db.Create(&settlementRate).Error
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		logrus.WithError(err).Errorf("添加代理基础结算信息失败: settlementRate:%v err:%v", settlementRate, err)
		return fmt.Errorf("添加代理基础结算信息失败")
	}
	return nil
}

// AgentSettlementRateAddSettlementInfo 添加代理结算信息
func AgentSettlementRateAddSettlementInfo(rate AgentSettlementRate) error {
	err := db.Create(&rate).Error
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		logrus.WithError(err).Errorf("添加代理结算信息失败: rate:%v err:%v", rate, err)
		return fmt.Errorf("添加代理结算信息失败")
	}
	return nil
}

// AgentSettlementRateUpdateSettlementInfo 更新代理结算信息
func AgentSettlementRateUpdateSettlementInfo(mapd map[string]interface{}, id interface{}) error {
	mapd["type"] = 1
	err := db.Model(&AgentSettlementRate{}).Where("id = ?", id).Updates(mapd).Error
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		logrus.WithError(err).Errorf("更新代理结算信息失败: mapd:%v err:%v", mapd, err)
		return fmt.Errorf("更新代理结算信息失败")
	}
	return nil
}

// AgentSettlementRateGetSettlementInfo 获取代理结算信息
func AgentSettlementRateGetSettlementInfo(agentId interface{}) (interface{}, error) {
	var result = new(agentResponse.TeamManagerAgentRateGetSettlementRateInfoResponse)
	err := db.Model(&AgentSettlementRate{}).Where("agent_id = ?", agentId).Scan(&result).Error
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		logrus.WithError(err).Errorf("更新获取代理结算信息失败: agentId:%s err:%v", agentId, err)
		return nil, fmt.Errorf("更新获取代理结算信息失败")
	}
	return result, nil
}
